package com.cabrida.livestar.mainstar;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.mainstar.StarCircleLvAdapter.CircleLikeListener;
import com.cabrida.livestar.model.FansCircleMsgChildrenData;
import com.cabrida.livestar.model.FansCircleMsgData;
import com.cabrida.livestar.model.StarCircleData;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;

/**
 * 明星端-空间
 * 
 * @author why
 */
public class StarSpaceView extends LinearLayout implements OnClickListener {
	private Context mContext;
	private LinearLayout layout_dynamice;
	private LinearLayout layout_msg;
	private TextView tv_title_dynamic;
	private View view_line_dynamic;
	private TextView tv_title_msg;
	private View view_line_msg;
	
	private LinearLayout ll_sendtrend;
	private ListView listView_dynamice;
	private StarCircleLvAdapter adapter;
	private ArrayList<StarCircleData> infoList=new ArrayList<StarCircleData>();
	
	private String clickMsgCid="";
	private String clickMsgCudid="";
	private String clickMsgCnickname="";
	private CircleImageView iv_header;
	private EditText et_content;
	private TextView tv_send;
	private LinearLayout ll_send;
	private ListView listView_msg;
	private CircleSpaceMsgLvAdapter adapter_msg;
	private ArrayList<FansCircleMsgData> infoList_msg=new ArrayList<FansCircleMsgData>();
	
	public StarSpaceView(Context context) {
		super(context);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_star_space, this);
		Init(view);
	}
	
	public void putLikeData(String cid, String number) {
		for (int i = 0; i < infoList.size(); i++) {
			if (infoList.get(i).getCid().equals(cid)) {
				infoList.get(i).setC_like(number);
			}
		}
		adapter.setData(infoList);
		adapter.notifyDataSetChanged();
	}
	
	public void putData(ArrayList<StarCircleData> list) {
		this.infoList = list;
		adapter.setData(infoList);
		adapter.notifyDataSetChanged();
	}
	public void putMsgData(ArrayList<FansCircleMsgData> list) {
		this.infoList_msg = list;
		adapter_msg.setData(infoList_msg);
		adapter_msg.notifyDataSetChanged();
	}
	

	/**
	 * 初始化控件
	 */
	private void Init(View view) {
		listView_dynamice = (ListView) view.findViewById(R.id.listView_dynamice);
		ll_sendtrend = (LinearLayout) view.findViewById(R.id.ll_sendtrend);
		layout_dynamice = (LinearLayout) view.findViewById(R.id.layout_dynamice);
		layout_msg = (LinearLayout) view.findViewById(R.id.layout_msg);
		tv_title_dynamic = (TextView) view.findViewById(R.id.tv_title_dynamic);
		view_line_dynamic = (View) view.findViewById(R.id.view_line_dynamic);
		tv_title_msg = (TextView) view.findViewById(R.id.tv_title_msg);
		view_line_msg = (View) view.findViewById(R.id.view_line_msg);
		ll_send = (LinearLayout) view.findViewById(R.id.ll_send);
		listView_msg = (ListView) view.findViewById(R.id.listView_msg);
		iv_header = (CircleImageView) view.findViewById(R.id.iv_header);
		MyUtils.loadImg(Global.headPic, iv_header);
		et_content = (EditText) view.findViewById(R.id.et_content);
		tv_send = (TextView) view.findViewById(R.id.tv_send);
		
		
		adapter = new StarCircleLvAdapter(mContext);
		listView_dynamice.setAdapter(adapter);
		adapter_msg = new CircleSpaceMsgLvAdapter(mContext);
		listView_msg.setAdapter(adapter_msg);
		tv_send.setOnClickListener(this);
		ll_sendtrend.setOnClickListener(this);
		tv_title_dynamic.setOnClickListener(this);
		tv_title_msg.setOnClickListener(this);
		
		listView_dynamice.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				StarCircleData data = (StarCircleData) arg0.getAdapter().getItem(arg2);
				Intent intent = new Intent();
				intent.setClass(mContext, CircleActivity.class);
				intent.putExtra("data", data);
				mContext.startActivity(intent);
			}
		});
		
		listView_msg.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				ll_send.setVisibility(View.VISIBLE);
				FansCircleMsgData clickmsgData = (FansCircleMsgData) arg0.getAdapter().getItem(arg2);
				clickMsgCid = clickmsgData.getCid();
				clickMsgCudid = clickmsgData.getC_udid();
				clickMsgCnickname = clickmsgData.getU_nickname();
				et_content.setText("回复"+clickMsgCnickname+":");
				et_content.setSelection(et_content.getText().length());
			}
		});
		
		adapter.setOnCircleLikeListener(new CircleLikeListener() {
			
			@Override
			public void onLike(String cid) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("cid", cid);
				intent.setAction(MainStarActivity.ACTION_CIRCLELIKE_LIST);
				mContext.sendBroadcast(intent);
			}
		});
		layout_dynamice.setVisibility(View.VISIBLE);
		layout_msg.setVisibility(View.GONE);
		view_line_dynamic.setVisibility(View.VISIBLE);
		view_line_msg.setVisibility(View.INVISIBLE);
		
		Intent intent = new Intent();
		intent.setAction(MainStarActivity.ACTION_CIRCLE_LIST);
		mContext.sendBroadcast(intent);
	}
	

	@Override
	public void onClick(View v) {
		Intent intent=null;
		switch (v.getId()) {
		case R.id.tv_title_dynamic:
			layout_dynamice.setVisibility(View.VISIBLE);
			layout_msg.setVisibility(View.GONE);
			view_line_dynamic.setVisibility(View.VISIBLE);
			view_line_msg.setVisibility(View.INVISIBLE);
			intent = new Intent();
			intent.setAction(MainStarActivity.ACTION_CIRCLE_LIST);
			mContext.sendBroadcast(intent);
			break;
		case R.id.tv_title_msg:
			layout_dynamice.setVisibility(View.GONE);
			layout_msg.setVisibility(View.VISIBLE);
			view_line_dynamic.setVisibility(View.INVISIBLE);
			view_line_msg.setVisibility(View.VISIBLE);
			intent = new Intent();
			intent.setAction(MainStarActivity.ACTION_MESSAGE_LIST);
			mContext.sendBroadcast(intent);
			break;
		case R.id.ll_sendtrend:
			intent = new Intent();
			intent.setClass(mContext, StarSendTrendActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.tv_send:
			String str_send = et_content.getText().toString().trim();
			if (TextUtils.isEmpty(str_send)||"".equals(str_send)) {
				Toast.makeText(mContext, "请输入回复内容!", Toast.LENGTH_SHORT).show();
				return;
			}
			ll_send.setVisibility(View.GONE);
			et_content.setText("");
			intent = new Intent();
			intent.setAction(MainStarActivity.ACTION_MESSAGE_BTN_CLICK);
			if (!TextUtils.isEmpty(clickMsgCid)&&!TextUtils.isEmpty(clickMsgCudid)&&!TextUtils.isEmpty(clickMsgCnickname)) {
				if (str_send.equals("回复"+clickMsgCnickname+":")) {
					Toast.makeText(mContext, "请输入回复内容!", Toast.LENGTH_SHORT).show();
					return;
				}
				if (str_send.startsWith("回复"+clickMsgCnickname+":")) {
					String strs[] = str_send.split("回复"+clickMsgCnickname+":");
					
					intent.putExtra("description", strs[1]);
					intent.putExtra("clickMsgCid",clickMsgCid);
					intent.putExtra("clickMsgCudid", clickMsgCudid);
					
				}else {
					intent.putExtra("description", str_send);
					intent.putExtra("clickMsgCid","");
					intent.putExtra("clickMsgCudid", "");
				}
			}else {
				intent.putExtra("description", str_send);
				intent.putExtra("clickMsgCid","");
				intent.putExtra("clickMsgCudid", "");
			}
			mContext.sendBroadcast(intent);
			break;
		default:
			break;
		}
	}

}
